package demo.annotation;

import lombok.extern.slf4j.Slf4j;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author zhanghaixuan
 * @date 2020-03-05 15:29
 */
@Slf4j
public class AnnotationParser {
    /**
     * 解析类的注解
     */

    public static  void parseTypeAnnotation() throws ClassNotFoundException {
        final Class<?> aClass = Class.forName("demo.annotation.ImoocCourse");

        final Annotation[] annotations = aClass.getAnnotations();

        for (Annotation annotation : annotations) {
            final CourseinfoAnnotation courseinfoAnnotation = (CourseinfoAnnotation) annotation;

            log.info("信息:{},{},{},{}",courseinfoAnnotation.courseType(),courseinfoAnnotation.courseName(),courseinfoAnnotation.courseProfile(),courseinfoAnnotation.courseIndex());
        }
    }

    public static void parseFieldAnnotation() throws ClassNotFoundException {
        final Class<?> aClass = Class.forName("demo.annotation.ImoocCourse");

        final Field[] declaredFields = aClass.getDeclaredFields();

        for (Field declaredField : declaredFields) {
            if(declaredField.isAnnotationPresent(PersonInfoAnnotation.class)){
                final PersonInfoAnnotation annotation = declaredField.getAnnotation(PersonInfoAnnotation.class);
                log.info("PersonInfoAnnotation的信息是:{},{},{},{}",annotation.age(),annotation.gender(),annotation.language(),annotation.name());
            }
        }
    }

    public static void parseMethodAnnotation() throws ClassNotFoundException {
        final Class<?> aClass = Class.forName("demo.annotation.ImoocCourse");

        final Method[] methods = aClass.getMethods();

        for (Method declaredField : methods) {
            if(declaredField.isAnnotationPresent(CourseinfoAnnotation.class)){
                final CourseinfoAnnotation annotation = declaredField.getAnnotation(CourseinfoAnnotation.class);
                log.info("CourseinfoAnnotation的信息是:{},{},{},{}",annotation.courseType(),annotation.courseName(),annotation.courseName(),annotation.courseType());
            }
        }
    }

    public static void main(String[] args) throws ClassNotFoundException {
        parseTypeAnnotation();
        parseFieldAnnotation();
        parseMethodAnnotation();
    }
}

